package com.ruoyi.web.utils;


import org.springframework.beans.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

public class RevertEntityUtil {
    public static <A, B> List<B> convertList(List<A> sourceList, Class<B> targetClass){
        List<B> targetList = new ArrayList<>();
        for (A sourceObject : sourceList) {
            B targetObject = null; // 使用无参构造函数创建B的实例
            try {
                targetObject = targetClass.getDeclaredConstructor().newInstance();
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
            BeanUtils.copyProperties(sourceObject, targetObject); // 复制属性
            targetList.add(targetObject);
        }
        return targetList;
    }
}
